class Solution
{
public:
    bool areSimilar(vector<vector<int>> &mat, int k)
    {
        int m = mat.size();
        int n = mat[0].size();
        k = k % n;
        for (int i = 0; i < m; ++i)
        {
            auto originRow = mat[i];
            if (i & 1)
            {
                for (int j = 0; j < k; ++j)
                {
                    int num = mat[i].back();
                    mat[i].pop_back();
                    mat[i].insert(mat[i].begin(), num);
                }
            }
            else
            {
                for (int j = 0; j < k; ++j)
                {
                    int num = mat[i][0];
                    mat[i].erase(mat[i].begin());
                    mat[i].push_back(num);
                }
            }
            if (originRow != mat[i])
            {
                return false;
            }
        }
        return true;
    }
};